<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>加减计数器</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f9;
        }

        .counter {
            display: flex;
            align-items: center;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .counter button {
            padding: 10px 20px;
            font-size: 18px;
            border: none;
            background-color: #007BFF;
            color: #fff;
            cursor: pointer;
            border-radius: 4px;
        }

        .counter button:hover {
            background-color: #0056b3;
        }

        .counter span {
            margin: 0 20px;
            font-size: 24px;
        }
    </style>
</head>

<body>
    <div class="counter">
        <button id="decrease">-</button>
        <span id="count">0</span>
        <button id="increase">+</button>
    </div>

    <script>
        // 获取元素
        const decreaseButton = document.getElementById('decrease');
        const increaseButton = document.getElementById('increase');
        const countElement = document.getElementById('count');

        // 初始化计数器值
        let count = 0;

        // 更新显示的计数器值
        function updateCount() {
            countElement.textContent = count;
        }

        // 点击减按钮的事件处理函数
        decreaseButton.addEventListener('click', function () {
            count--;
            updateCount();
        });

        // 点击加按钮的事件处理函数
        increaseButton.addEventListener('click', function () {
            count++;
            updateCount();
        });

        // 初始更新显示
        updateCount();
    </script>
</body>

</html>